<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Json;
/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Rbac-用户管理';
$this->params['breadcrumbs'][] = ['label' => 'Rbac', 'url' => ['user/index']];
$this->params['breadcrumbs'][] = '用户管理';
Url::remember();
?>
<div class="layui-card">
    <div class="layui-form layui-form-pane layui-card-header layuiadmin-card-header-auto">
        <?php $form = ActiveForm::begin([
            'id' => 'search-form',
            'method'=>'get',
            'action'=>Url::to('/rbac/user/index'),
            'options'=>['lay-filter'=>'form-search'],
            'fieldConfig' => [
                'options' => [
                    'tag' => false, //不生成上一层的<div class='form-group'>
                ],
            ],
        ]); ?>
        <div class="layui-form-item">
            <?= $form->field($model, 'id', [
                'template' => "{label}<div class=\"layui-input-inline\">{input}</div>",
                'labelOptions' => ['class'=>'layui-form-label'],
            ])->textInput(['class'=>'layui-input']) ?>
            <?= $form->field($model, 'username', [
                'template' => "{label}<div class=\"layui-input-inline\">{input}</div>",
                'labelOptions' => ['class'=>'layui-form-label']
            ])->textInput(['class'=>'layui-input']) ?>
            <div class="layui-inline">
                <?= Html::submitButton('搜索', ['class'=>"layui-btn", 'lay-submit'=>''])?>
            </div>
        </div>
        <?php ActiveForm::end(); ?>
    </div>
    <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
            <?=Html::a('新建', Url::to('create'), ['class'=>'layui-btn'])?>
        </div>
        <table id="index-content" lay-filter="index-content"></table>
        <script type="text/html" id="toolBar">
<!--            <a class="layui-btn layui-btn-xs" lay-event="view">-->
<!--                <i class="layui-icon layui-icon-form"></i>查看-->
<!--            </a>-->
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="update">
                <i class="layui-icon layui-icon-edit"></i>编辑
            </a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">
                <i class="layui-icon layui-icon-delete"></i>删除
            </a>
        </script>
    </div>
</div>
<script>
    <?php $this->beginBlock('js') ?>
    layui.use(['table'], function () {
        var table = layui.table;
        table.render({
            elem: '#index-content'
            ,url:'<?=Url::to()?>'
            ,cols: [[
                {field:'id', width:'', title: 'ID'}
                ,{field:'username', width:'', title: '用户名', sort: true}
                ,{field:'email', width:'', title: '邮箱', sort: true}
                ,{field:'created_at', width:'', title: '创建时间', sort: true}
                ,{field:'updated_at', width:'', title: '更新时间', sort: true}
                ,{fixed:'right', title:'操作', toolbar: '#toolBar', width:''}
            ]]
            ,page: true
        });
        table.on('tool(index-content)', function(obj){
            var url;
            switch(obj.event){
                case 'view':
                    url = '<?=Url::to('view')?>?id=' + obj.data.id;
                    window.location.href = url;
                    break;
                case 'update':
                    url = '<?=Url::to('update')?>?id=' + obj.data.id;
                    window.location.href = url;
                    break;
                case 'delete':
                    layer.confirm('确认要删除吗?', function(index){
                        layer.close(index);
                        $.ajax({
                            url:'<?=Url::to('delete?id=')?>' + obj.data.id,
                            type:'post',
                            success:function (result) {
                            if(result.status == 200){
                                obj.del();
                                layer.alert('<div style="text-align: center">删除成功</div>', {
                                    title: '提示信息'
                                });
                            }else{
                                layer.alert('<div style="text-align: center">' + result.msg + '</div>', {
                                    title: '提示信息'
                                });
                            }
                        },
                        error:function (result) {
                            layer.alert('<div style="text-align: center">网络错误,请重试</div>', {
                                title: '提示信息'
                            });
                        }
                    });
                    });
                    break;
                default:
                    break;
            };
        });
    });
    <?php $this->endBlock() ?>
    <?php $this->registerJs($this->blocks['js'], \yii\web\View::POS_END); ?>
</script>
